using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

using TvdbLib;
using TvdbLib.Cache;
using TvdbLib.Data;

namespace MCEMediaManager
{
    public class TVDB : IDisposable
    {
        public TVDB()
        {
            if (!Directory.Exists(XmlCacheLocation))
            {
                Directory.CreateDirectory(XmlCacheLocation);
            }
        }
        private Dictionary<string, TvdbEpisode> _EpisodeCache;
        public Dictionary<string, TvdbEpisode> EpisodeCache
        {
            get
            {
                if (_EpisodeCache == null)
                {
                    _EpisodeCache = new Dictionary<string, TvdbEpisode>();
                }
                return _EpisodeCache;
            }
            set
            {
                _EpisodeCache = value;
            }
        }
        private Dictionary<string, int> _SeriesNameLookup;
        public Dictionary<string, int> SeriesNameLookup
        {
            get
            {
                if (_SeriesNameLookup == null)
                {
                    _SeriesNameLookup = new Dictionary<string, int>();
                }
                return _SeriesNameLookup;
            }
            set { _SeriesNameLookup = value; }
        }
        public static string APIKey = "14B7E96893F36EAC";
        public static string XmlCacheLocation
        {
            get
            {
                return Application.Current.SharedDirectory + @"TVDBCache\";
            }
        }
        private TvdbHandler _Handler;
        public TvdbHandler Handler
        {
            get
            {
                if (_Handler == null)
                {
                    _Handler = new TvdbHandler(new XmlCacheProvider(XmlCacheLocation), APIKey);
                    _Handler.InitCache();
                }
                return _Handler;
            }
        }
        public TvdbEpisode GetEpisode(string seriesName, int seasonNum, int episodeNum)
        {
            TvdbEpisode episode = null;

            int seriesId = 0;
            if (SeriesNameLookup.ContainsKey(seriesName))
                seriesId = SeriesNameLookup[seriesName];
            else
            {
                List<TvdbSearchResult> seriesList = Handler.SearchSeries(seriesName);
                foreach (TvdbSearchResult series in seriesList)
                {
                    if (series.SeriesName.ToUpper() == seriesName.ToUpper())
                    {
                        SeriesNameLookup.Add(series.SeriesName, series.Id);
                        seriesId = series.Id;
                        break;
                    }
                }
            }
            if (seriesId != 0)
            {
                if (EpisodeCache.ContainsKey(seriesId + "|" + seasonNum + "|" + episodeNum))
                {
                    episode = EpisodeCache[seriesId + "|" + seasonNum + "|" + episodeNum];
                }
                else
                {
                    episode = Handler.GetEpisode(seriesId, seasonNum, episodeNum, TvdbEpisode.EpisodeOrdering.DefaultOrder, TvdbLanguage.DefaultLanguage);
                    EpisodeCache.Add(seriesId + "|" + seasonNum + "|" + episodeNum, episode);
                }
            }
            return episode;
        }

        public void Dispose()
        {
            if (_Handler != null)
                Handler.CloseCache();
        }
    }
}
